package main

import (
	"fmt"
	"math"
	"math/cmplx"
	"reflect"
)

// 包内定义多个变量
var (
	aa = 3
	ss = "kkk"
	bb = true
)

// 默认值
func variableZeroValue() {
	var a int
	var s string
	fmt.Printf("variable zero value:%d, %q \n", a, s) // %d:十进制表示 %q:字符串表示
}

func variableInitialValue() {
	var a, b int = 3, 4
	var s string = "abc"
	c, d := 5, 6
	fmt.Println("variable initial value", c, d)
	c, e := 6, 7 // 这里的c 属于重新赋值
	{
		c := 8
		fmt.Println("variable initial value", c)
	}
	fmt.Println("vairable initial value:", c, e)
	fmt.Println(a, b, s)
}

func variableTypeDeduction() {
	var a, b, c, s = 3, 4, true, "def"
	fmt.Println(a, b, c, s)
}

// 反射获取变量类型
func variableTypeOf() {
	a1 := "hello"
	fmt.Println(reflect.TypeOf(a1))
	a2 := 3
	fmt.Println(reflect.TypeOf(a2))
	a3 := 3.0
	fmt.Println(reflect.TypeOf(a3))
	a4 := &a3
	fmt.Println(reflect.TypeOf(a4))
}

var as = 1 // 包内定义变量，且只能用var不能用 :=
func variableShorter() {
	a, b, c, s := 3, 4, true, "def" // 在方法内定义变量 :=
	b = 5
	fmt.Println(a, b, c, s)
}

// 变量的类型转换,会导致精度丢失
func variableTypeConvert() {
	var n6 uint = math.MaxUint64 // 无符号int64最大
	fmt.Println("variable type convert", n6)
	var n7 = int(n6) // out put -1
	fmt.Println("variable type convert", n7)
}

func euler() {
	fmt.Printf("%.3f\n", cmplx.Exp(1i*math.Pi)+1)
}

func triangle() {
	var a, b int = 3, 4
	fmt.Println(calcTriangle(a, b))
}

func calcTriangle(a, b int) int {
	var c int
	c = int(math.Sqrt(float64(a*a + b*b)))
	return c
}

func consts() {
	const (
		filename = "abc.txt"
		a, b     = 3, 4
	)
	var c int
	c = int(math.Sqrt(a*a + b*b))
	fmt.Println(filename, c)
}

func enums() {
	const (
		cpp = iota // 有序0，1，2，3，4
		_
		python
		golang
		javascript
	)

	const (
		b = 1 << (10 * iota)
		kb
		mb
		gb
		tb
		pb
	)

	fmt.Println(cpp, javascript, python, golang)
	fmt.Println(b, kb, mb, gb, tb, pb)
}

func main1() {
	fmt.Println("Hello world")
	variableZeroValue()
	variableInitialValue()
	variableTypeDeduction()
	variableShorter()
	fmt.Println(aa, ss, bb)

	euler()
	triangle()
	consts()
	enums()
}
